package signGrammar.expressions;

import signGrammar.IShape;
import signGrammar.expressions.util.CExpressionElement;
import signGrammar.expressions.util.CExpressionElementArrayBuilder;
import signGrammar.expressions.util.UExpression;
import signGrammar.util.UShapeExpression;
import static signGrammar.expressions.util.CExpressionElement._;

public class CBinaryExpression implements IGrammarExpression{
	private IGrammarExpression left, right;
	private String binaryExpressionSign;
	public CBinaryExpression(IGrammarExpression left, String binaryExpressionSign, IGrammarExpression right) {
		this.left = left;
		this.right = right;
		this.binaryExpressionSign = binaryExpressionSign;
	}

	@Override
	public IShape evaluate(IGrammarContext context) {
		return UShapeExpression.eval(left.evaluate(context), binaryExpressionSign, right.evaluate(context));
	}

	@Override
	public IGrammarExpression replace(String name, IGrammarExpression expression) {
		return UExpression.eval(left.replace(name, expression), binaryExpressionSign, right.replace(name, expression));
	}

	@Override
	public IGrammarExpression clone(){
		return UExpression.eval(left, binaryExpressionSign, right);
	}
	
	@Override
	public String toString(){
		return "(" + left.toString() + " " + binaryExpressionSign + " " + right.toString() + ")";
	}
	
	@Override
	public CExpressionElement[] toElements(){
		return new CExpressionElementArrayBuilder()
			.add(_("("))
			.add( left.toElements() )
			.add(_(binaryExpressionSign))
			.add( right.toElements() )
			.add(_(")"))
			.get();
	}
}
